package org.ljy.rabbitextensionsdemo.listener;


import com.rabbitmq.client.Channel;
import org.ljy.rabbitextensionsdemo.constant.Constant;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;




@Component
public class AckListener {



    //@RabbitListener(queues = Constant.ACK_QUEUE)
    public void ackQueue1(Message message, Channel channel) throws IOException {
        System.out.println("消息是："+new String(message.getBody()));
        System.out.println("deliveryTag下标是："+message.getMessageProperties().getDeliveryTag());
        System.out.println("消费异常前");
        //int a=10/0;
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        System.out.println("消费异常后");
    }

    @RabbitListener(queues = Constant.ACK_QUEUE)
    public void ackQueue2(Message message, Channel channel) throws IOException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.println("消息是："+new String(message.getBody()));
        System.out.println("deliveryTag下标是："+deliveryTag);
        try {
            System.out.println("消费异常前");
            int a=10/0;
            System.out.println("消费异常后");
            channel.basicAck(deliveryTag, false);
        }catch (Exception e) {
            System.out.println("消费异常");
            channel.basicNack(deliveryTag, false, true);
        }
    }


    //@RabbitListener(queues = Constant.ACK_QUEUE)
    public void ackQueue(Message message, Channel channel) throws IOException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();//消息的序号
        try {
            System.out.println("异常情况前");
            //int a=10/0;
            System.out.println("异常情况后");
            System.out.println("接受到消息："+ new String(message.getBody()));
            channel.basicAck(deliveryTag, false);//收到确认消息
            channel.basicReject(deliveryTag, false);
        }catch (Exception e) {
            channel.basicNack(deliveryTag,false,true);//b:非批量确认，b1：不进行重新入队
            //System.out.println("尝试重新入队列");
        }
    }



    //@RabbitListener(queues = Constant.RETRY_QUEUE)
    public void retryQueue1(Message message, Channel channel) throws IOException {
        System.out.println("retry接口测试：");
        System.out.println("retry:"+new String(message.getBody()));

        //异常情况
        int a=9/0;

    }

    //@RabbitListener(queues = Constant.RETRY_QUEUE)
    public void retryQueue2(Message message, Channel channel) throws IOException {
        System.out.println("retry接口测试：");
        System.out.println("retry:"+new String(message.getBody()));
        //异常情况
        try{
           int a=9/0;
            System.out.println("消费出现异常");
        }catch (Exception e) {
            System.out.println("retry:捕获到异常情况");
        }

    }


}